package com.paas.demo.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static String FILE_NAME = "MMddHHmmssSSS";
	public static String DEFAULT_PATTERN = "yyyy-MM-dd";
	public static String DIR_PATTERN = "yyyy/MM/dd/";
	public static String TIMESTAMP_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static String TIMES_PATTERN = "HH:mm:ss";
	public static String NOCHAR_PATTERN = "yyyyMMddHHmmss";
	/**
	* 日期转换为字符串
	*
	* @param date
	* 日期
	* @param format
	* 日期格式
	* @return 指定格式的日期字符串
	*/
	public static String formatDate(Date date, String format) {
	String result = "";
	if (date != null) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			result = sdf.format(date);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	return result;
	}
	/**
	* 日期格式字符串转换为日期对象
	*
	* @param strDate
	* 日期格式字符串
	* @param pattern
	* 日期对象
	* @return
	*/
	public static Date parseDate(String strDate, String pattern) {
	try {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date nowDate = format.parse(strDate);
		return nowDate;
	} catch (Exception e) {
		e.printStackTrace();
	}
	return null;
	}
	/**
	 * 将秒数转成Date
	 * 
	 * @param sec
	 * @return
	 */
	public static Date parseDate(long sec){
		return new Date(sec*1000);
	}
	/**
	* 日期相减(返回天数)
	*
	* @param date
	* Date
	*
	* @param date1
	* Date
	*
	* @return int 相差的天数
	*/
	public static int diffDate(Date end_date, Date begin_date) {
		return (int) ((getMillis(end_date) - getMillis(begin_date)) / (24 * 3600 * 1000));
	}
	/**
	* 日期增加
	*
	* @param date
	* Date
	*
	* @param day
	* int
	*
	* @return Date
	*/
	public static Date addDate(Date date, int day) {
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
		return c.getTime();
	}
	/**
	* 获取当前毫秒
	*
	* @param date
	*
	* @return
	*/
	public static long getMillis(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c.getTimeInMillis();
	}
	public static long getSecond(Date data){
		if (data == null) {
			return 0;
		}
		return data.getTime()/1000;
	}

	/**
	 * 获取当天开始时间 00:00:00
	 * @param date
	 * @return
	 */
	public static Date getDayBegin(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
	    c.set(Calendar.HOUR_OF_DAY, 0);
	    c.set(Calendar.MINUTE, 0);
	    c.set(Calendar.SECOND, 0);
	    return c.getTime();
	}
	/**
	 * 获取当天结束时间23:59:59
	 * @param date
	 * @return
	 */
	public static Date getDayEnd(Date date){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
	    c.set(Calendar.HOUR_OF_DAY, 23);
	    c.set(Calendar.MINUTE, 59);
	    c.set(Calendar.SECOND, 59);
	    return c.getTime();
	}
	public static void main(String[] args) {

	    System.out.println(DateUtil.parseDate("2018/6/29 19:55:47", "yyyy/MM/dd HH:mm:ss"));

	    System.out.println(DateUtil.getDayEnd(new Date()));
	}
	
	
	
    public static final int MILLIS_TO_SECONDS = 1000;

    public static final int MILLIS_TO_MINUTES = 60 * 1000;
    
    public static final int MILLIS_TO_DAY = 24 * 60 * 60 * 1000;
    /**
     * 获取当前分钟数
     * 
     * @return
     */
    public static long getCurrentTimeOfMinutes() {
        return System.currentTimeMillis() / MILLIS_TO_MINUTES;
    }

    /**
     * 获取当前天数
     * 
     * @return
     */
    public static long getCurrentTimeOfDay() {
        return System.currentTimeMillis() / MILLIS_TO_DAY;
    }
}

